var $ = require("../../utils/Ajax.js");
function GetOrders(that,Status){
  $.Ajax("api/order","GET",{
    s: Status
  },function(res){
    if(res.s){
      that.setData({
        Order: res.d.orders
      })
    }else{
      wx.showToast({
        title: res.m,
        icon: 'none',
        duration: 1500
      })
    }
    console.log(res)
  })
}

Page({
  data: {
    // 图片前缀路径 - (全局)
    PictureUrl: getApp().PictureUrl,
    NavList: ["待付款", "待发货", "待收货", "待评价", "全部"],
    ListIndex: 0,
    OrderStatus: null,
    Order: [],
    CancelOrder: ['我不想买了', '信息填写错误,重新拍', '卖家缺货', '同城见面交易', '其他原因'],
  },

  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "我的订单"
    })
    this.setData({
      ListIndex: options.Status
    })
  },

  onShow: function () {
    GetOrders(this, this.data.ListIndex);
  },

  // 点击订单列表
  ClickItem: function (e) {
    var Index = e.currentTarget.dataset.index_;
    GetOrders(this, Index);
    this.setData({
      ListIndex: Index
    })
  },

  // 点击取消订单
  CancelOrder: function(e){
    var that = this;
    var Id = e.currentTarget.dataset.id;
    var Remark = that.data.CancelOrder[e.detail.value];
    $.Ajax("api/cancelOrder","POST",{
      id: Id,
      remark: Remark
    },function(res){
      wx.showToast({
        title: res.m,
        icon: 'none',
        duration: 1500
      })
      if(res.s){
        GetOrders(that, that.data.ListIndex);
      }
    })
  },

  // 点击换货
  Barter: function(e){
    var Goods = JSON.stringify(e.currentTarget.dataset.goods);
    wx.navigateTo({
      url: '../Barter/Barter?Goods=' + Goods,
    })
  },

  // 点击退货退款
  Refunds: function(e){
    var Goods = JSON.stringify(e.currentTarget.dataset.goods);
    wx.navigateTo({
      url: '../Refunds/Refunds?Goods=' + Goods,
    })
  },

  // 点击查看物流
  Logistics: function(e){
    var Id = e.currentTarget.dataset.id;
    $.Ajax("api/shipping","POST",{
      id: Id
    },function(res){
      if (res.result){
        wx.showModal({
          title: '物流信息',
          content: '描述文字尽量控制在三行内弹窗内容，\r\n \r\n \r\n',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            }
          }
        });
      }else{
        wx.showToast({
          title: res.message,
          icon: 'none',
          duration: 3000
        })
      }
    })
  },

  // 确认收货
  TakeGoods: function(e){
    var that = this;
    var Id = e.currentTarget.dataset.id;
    $.Ajax("api/confirmOrder","POST",{
      id: Id
    },function(res){
      wx.showToast({
        title: res.m,
        icon: 'none',
        duration: 1500
      })
      if(res.s){  
        GetOrders(that, that.data.ListIndex);
      }
    })
  },

  // 删除订单
  DelOrder: function(e){
    var that = this;
    var Id = e.currentTarget.dataset.id;
    $.Ajax("api/delOrder","POST",{
      id: Id
    },function(res){
      wx.showToast({
        title: res.m,
        icon: 'none',
        duration: 1500
      })
      if (res.s) {
        GetOrders(that, that.data.ListIndex);
      }
    })
  },

  // 点击申请售后
  AfterSales: function(e){
    var Goods = JSON.stringify(e.currentTarget.dataset.goods);
    wx.navigateTo({
      url: '../AfterSales/AfterSales?Goods=' + Goods,
    })
  },

  // 点击评价
  Comment: function(e){
    var Id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../Comment/Comment?Id=' + Id,
    })
  }
})